package managedBean.admin;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;
import org.primefaces.showcase.domain.Car;

import utility.ApplicationUtils;
import controller.admin.ProfileController;
import controller.search.SearchController;
import dto.search.ProfileDTO;
import dto.search.SearchDTO;

@ManagedBean(name = "profileManagedBean")
@ViewScoped
public class ProfileManagedBean implements Serializable {

	private static final long serialVersionUID = -977158227338727636L;
	@ManagedProperty(value = "#{profileController}")
	private ProfileController profileController;
	@ManagedProperty(value = "#{searchController}")
	private SearchController searchController;
	private ProfileDTO profileDTO;
	private int profileId;
	private String tenantid;
	private String firstname;
	private String lastname;
	private String gender;
	private Date dob;
	private String age;
	private String birthplace;
	private Integer height;
	private String complexion;
	private String educationlevel;
	private String education;
	private String maritalstatus;
	private String paternalgotram;
	private String maternalgotram;
	private String star;
	private String rasi;
	private String aboutself;
	private String occupation;
	private String income;
	private String fathername;
	private String fatherprofession;
	private String mothername;
	private String motherprofession;
	private String email;
	private String mobileno;
	private String address;
	private String url;
	private String videoUrl;
	private List<SelectItem> incomeList;
	private List<SelectItem> heightList;
	private List<SelectItem> starList;
	private List<SelectItem> rasiList;
	private List<SelectItem> maritalstatusList;
	private List<SelectItem> educationLevelList;
	private String id;
	private List<ProfileDTO> list;
	private List<ProfileDTO> filteredProfiles;
	private ProfileDTO selectedProfile;
	@PostConstruct
	public void init() {
		profileDTO = new ProfileDTO();
		incomeList = ApplicationUtils.initializeIncomeList();
		heightList = ApplicationUtils.initializeHeightList();
		starList = ApplicationUtils.initializeStarList();
		rasiList = ApplicationUtils.initializeRasiList();
		maritalstatusList=ApplicationUtils.initializeMaritalStatusList();
		educationLevelList=ApplicationUtils.initializeEducationLevelList();
		SearchDTO searchDTO=new SearchDTO();
		searchDTO.setTenantID("8P2");
		try {
			list = searchController.searchByQuery(searchDTO);
		} catch (Exception e) {
			FacesContext facesContext = FacesContext.getCurrentInstance();
			facesContext.addMessage(null, new FacesMessage(
					"Exception occured while fetching data "));
			e.printStackTrace();
		}
	}

	public void addProfile() {
		ProfileDTO profileDTO = copyProperties();
		profileDTO.setTenantid("8P2");//getTenantid());
		profileDTO.setReccrtts(new Date());
		profileDTO.setReclstupdatets(new Date());
		FacesContext facesContext = FacesContext.getCurrentInstance();
		try {
			Integer profileId=profileController.addProfile(profileDTO);
			facesContext.addMessage(null, new FacesMessage(
					"Profile Added Successfully "+profileId));
		} catch (Exception e) {
			e.printStackTrace();
			facesContext.addMessage(null, new FacesMessage(
					"Problem Occured while adding"));
		}
	}

	public void reset(){
		RequestContext.getCurrentInstance().reset("addForm");
	}
	public void search() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		try {
			profileDTO = searchController.searchByID(Integer.parseInt(id),
					"8P2");
			copyPropertiesfromDTO(profileDTO); 
		} catch (NumberFormatException e) {
			facesContext.addMessage(null, new FacesMessage(
					"Please enter a valid Profile ID "+profileId));
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			facesContext.addMessage(null, new FacesMessage(
					"Please enter a valid Profile ID "+profileId));
			e.printStackTrace();
		}
	}
	public void editProfile() {
		ProfileDTO profileDTO =copyProperties();
		profileDTO.setIdProfile(getProfileId());
		profileDTO.setReclstupdatets(new Date());
		FacesContext facesContext = FacesContext.getCurrentInstance();
		try {
			profileController.editProfile(profileDTO);
			facesContext.addMessage(null, new FacesMessage(
					"Profile Edited Successfully "+profileId));
		} catch (Exception e) {
			e.printStackTrace();
			facesContext.addMessage(null, new FacesMessage(
					"Problem Occured while Editin"));
		}
	}
	public void onRowSelect(SelectEvent event) {
        FacesMessage msg = new FacesMessage("Profile Selected", ((ProfileDTO) event.getObject()).getIdProfile()+"");
        FacesContext.getCurrentInstance().addMessage(null, msg);
        copyPropertiesfromDTO(((ProfileDTO) event.getObject()));
    }
	private ProfileDTO copyProperties(){
		ProfileDTO profileDTO = new ProfileDTO();
		profileDTO.setAboutself(getAboutself());
		profileDTO.setAddress(getAddress());
		profileDTO.setAge(getAge());
		profileDTO.setBirthplace(getBirthplace());
		profileDTO.setComplexion(getComplexion());
		profileDTO.setDob(getDob());
		profileDTO.setEducation(getEducation());
		profileDTO.setEducationlevel(getEducationlevel());
		profileDTO.setEmail(getEmail());
		profileDTO.setFathername(getFathername());
		profileDTO.setFatherprofession(getFatherprofession());
		profileDTO.setFirstname(getFirstname());
		profileDTO.setGender(getGender());
		profileDTO.setHeight(getHeight());
		profileDTO.setIncome(getIncome());
		profileDTO.setLastname(getLastname());
		profileDTO.setMaritalstatus(getMaritalstatus());
		profileDTO.setMaternalgotram(getMaternalgotram());
		profileDTO.setMobileno(getMobileno());
		profileDTO.setMothername(getMothername());
		profileDTO.setMotherprofession(getMotherprofession());
		profileDTO.setOccupation(getOccupation());
		profileDTO.setPaternalgotram(getPaternalgotram());
		profileDTO.setRasi(getRasi());
		profileDTO.setStar(getStar());
		profileDTO.setTenantid(getTenantid());
		return profileDTO;
	}
	
	private void copyPropertiesfromDTO(ProfileDTO profileDTO){
		
		setAboutself(profileDTO.getAboutself());
		setAddress(profileDTO.getAddress());
		setAge(profileDTO.getAge());
		setBirthplace(profileDTO.getBirthplace());
		setComplexion(profileDTO.getComplexion());
		setDob(profileDTO.getDob());
		setEducation(profileDTO.getEducation());
		setEducationlevel(profileDTO.getEducationlevel());
		setEmail(profileDTO.getEmail());
		setFathername(profileDTO.getFathername());
		setFatherprofession(profileDTO.getFatherprofession());
		setFirstname(profileDTO.getFirstname());
		setGender(profileDTO.getGender());
		setHeight(profileDTO.getHeight());
		setProfileId(profileDTO.getIdProfile());
		setIncome(profileDTO.getIncome());
		setLastname(profileDTO.getLastname());
		setMaritalstatus(profileDTO.getMaritalstatus());
		setMaternalgotram(profileDTO.getMaternalgotram());
		setMobileno(profileDTO.getMobileno());
		setMothername(profileDTO.getMothername());
		setMotherprofession(profileDTO.getMotherprofession());
		setOccupation(profileDTO.getOccupation());
		setPaternalgotram(profileDTO.getPaternalgotram());
		setRasi(profileDTO.getRasi());
		setStar(profileDTO.getStar());
		setTenantid(profileDTO.getTenantid());
	}
	public void deleteProfile() {

	}

	/**
	 * @return the controller
	 */
	public ProfileController getController() {
		return profileController;
	}

	/**
	 * @param controller
	 *            the controller to set
	 */
	public void setController(ProfileController profileController) {
		this.profileController = profileController;
	}

	/**
	 * @return the profileDTO
	 */
	public ProfileDTO getProfileDTO() {
		return profileDTO;
	}

	/**
	 * @param profileDTO
	 *            the profileDTO to set
	 */
	public void setProfileDTO(ProfileDTO profileDTO) {
		this.profileDTO = profileDTO;
	}

	/**
	 * @return the serialversionuid
	 */
	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	/**
	 * @return the tenantid
	 */
	public String getTenantid() {
		return tenantid;
	}

	/**
	 * @param tenantid
	 *            the tenantid to set
	 */
	public void setTenantid(String tenantid) {
		this.tenantid = tenantid;
	}

	/**
	 * @return the firstname
	 */
	public String getFirstname() {
		return firstname;
	}

	/**
	 * @param firstname
	 *            the firstname to set
	 */
	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	/**
	 * @return the lastname
	 */
	public String getLastname() {
		return lastname;
	}

	/**
	 * @param lastname
	 *            the lastname to set
	 */
	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	/**
	 * @return the gender
	 */
	public String getGender() {
		return gender;
	}

	/**
	 * @param gender
	 *            the gender to set
	 */
	public void setGender(String gender) {
		this.gender = gender;
	}

	/**
	 * @return the dob
	 */
	public Date getDob() {
		return dob;
	}

	/**
	 * @param dob
	 *            the dob to set
	 */
	public void setDob(Date dob) {
		this.dob = dob;
	}

	/**
	 * @return the age
	 */
	public String getAge() {
		return age;
	}

	/**
	 * @param age
	 *            the age to set
	 */
	public void setAge(String age) {
		this.age = age;
	}

	/**
	 * @return the birthplace
	 */
	public String getBirthplace() {
		return birthplace;
	}

	/**
	 * @param birthplace
	 *            the birthplace to set
	 */
	public void setBirthplace(String birthplace) {
		this.birthplace = birthplace;
	}

	/**
	 * @return the height
	 */
	public Integer getHeight() {
		return height;
	}

	/**
	 * @param height
	 *            the height to set
	 */
	public void setHeight(Integer height) {
		this.height = height;
	}

	/**
	 * @return the complexion
	 */
	public String getComplexion() {
		return complexion;
	}

	/**
	 * @param complexion
	 *            the complexion to set
	 */
	public void setComplexion(String complexion) {
		this.complexion = complexion;
	}

	/**
	 * @return the educationlevel
	 */
	public String getEducationlevel() {
		return educationlevel;
	}

	/**
	 * @param educationlevel
	 *            the educationlevel to set
	 */
	public void setEducationlevel(String educationlevel) {
		this.educationlevel = educationlevel;
	}

	/**
	 * @return the education
	 */
	public String getEducation() {
		return education;
	}

	/**
	 * @param education
	 *            the education to set
	 */
	public void setEducation(String education) {
		this.education = education;
	}

	/**
	 * @return the maritalstatus
	 */
	public String getMaritalstatus() {
		return maritalstatus;
	}

	/**
	 * @param maritalstatus
	 *            the maritalstatus to set
	 */
	public void setMaritalstatus(String maritalstatus) {
		this.maritalstatus = maritalstatus;
	}

	/**
	 * @return the paternalgotram
	 */
	public String getPaternalgotram() {
		return paternalgotram;
	}

	/**
	 * @param paternalgotram
	 *            the paternalgotram to set
	 */
	public void setPaternalgotram(String paternalgotram) {
		this.paternalgotram = paternalgotram;
	}

	/**
	 * @return the maternalgotram
	 */
	public String getMaternalgotram() {
		return maternalgotram;
	}

	/**
	 * @param maternalgotram
	 *            the maternalgotram to set
	 */
	public void setMaternalgotram(String maternalgotram) {
		this.maternalgotram = maternalgotram;
	}

	/**
	 * @return the star
	 */
	public String getStar() {
		return star;
	}

	/**
	 * @param star
	 *            the star to set
	 */
	public void setStar(String star) {
		this.star = star;
	}

	/**
	 * @return the rasi
	 */
	public String getRasi() {
		return rasi;
	}

	/**
	 * @param rasi
	 *            the rasi to set
	 */
	public void setRasi(String rasi) {
		this.rasi = rasi;
	}

	/**
	 * @return the aboutself
	 */
	public String getAboutself() {
		return aboutself;
	}

	/**
	 * @param aboutself
	 *            the aboutself to set
	 */
	public void setAboutself(String aboutself) {
		this.aboutself = aboutself;
	}

	/**
	 * @return the occupation
	 */
	public String getOccupation() {
		return occupation;
	}

	/**
	 * @param occupation
	 *            the occupation to set
	 */
	public void setOccupation(String occupation) {
		this.occupation = occupation;
	}

	/**
	 * @return the income
	 */
	public String getIncome() {
		return income;
	}

	/**
	 * @param income
	 *            the income to set
	 */
	public void setIncome(String income) {
		this.income = income;
	}

	/**
	 * @return the fathername
	 */
	public String getFathername() {
		return fathername;
	}

	/**
	 * @param fathername
	 *            the fathername to set
	 */
	public void setFathername(String fathername) {
		this.fathername = fathername;
	}

	/**
	 * @return the fatherprofession
	 */
	public String getFatherprofession() {
		return fatherprofession;
	}

	/**
	 * @param fatherprofession
	 *            the fatherprofession to set
	 */
	public void setFatherprofession(String fatherprofession) {
		this.fatherprofession = fatherprofession;
	}

	/**
	 * @return the mothername
	 */
	public String getMothername() {
		return mothername;
	}

	/**
	 * @param mothername
	 *            the mothername to set
	 */
	public void setMothername(String mothername) {
		this.mothername = mothername;
	}

	/**
	 * @return the motherprofession
	 */
	public String getMotherprofession() {
		return motherprofession;
	}

	/**
	 * @param motherprofession
	 *            the motherprofession to set
	 */
	public void setMotherprofession(String motherprofession) {
		this.motherprofession = motherprofession;
	}

	/**
	 * @return the email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * @param email
	 *            the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * @return the mobileno
	 */
	public String getMobileno() {
		return mobileno;
	}

	/**
	 * @param mobileno
	 *            the mobileno to set
	 */
	public void setMobileno(String mobileno) {
		this.mobileno = mobileno;
	}

	/**
	 * @return the address
	 */
	public String getAddress() {
		return address;
	}

	/**
	 * @param address
	 *            the address to set
	 */
	public void setAddress(String address) {
		this.address = address;
	}

	/**
	 * @return the url
	 */
	public String getUrl() {
		return url;
	}

	/**
	 * @param url
	 *            the url to set
	 */
	public void setUrl(String url) {
		this.url = url;
	}

	/**
	 * @return the videoUrl
	 */
	public String getVideoUrl() {
		return videoUrl;
	}

	/**
	 * @param videoUrl
	 *            the videoUrl to set
	 */
	public void setVideoUrl(String videoUrl) {
		this.videoUrl = videoUrl;
	}

	/**
	 * @return the profileController
	 */
	public ProfileController getProfileController() {
		return profileController;
	}

	/**
	 * @param profileController
	 *            the profileController to set
	 */
	public void setProfileController(ProfileController profileController) {
		this.profileController = profileController;
	}

	/**
	 * @return the profileId
	 */
	public int getProfileId() {
		return profileId;
	}

	/**
	 * @param profileId
	 *            the profileId to set
	 */
	public void setProfileId(int profileId) {
		this.profileId = profileId;
	}

	/**
	 * @return the incomeList
	 */
	public List<SelectItem> getIncomeList() {
		return incomeList;
	}

	/**
	 * @param incomeList
	 *            the incomeList to set
	 */
	public void setIncomeList(List<SelectItem> incomeList) {
		this.incomeList = incomeList;
	}

	/**
	 * @return the heightList
	 */
	public List<SelectItem> getHeightList() {
		return heightList;
	}

	/**
	 * @param heightList
	 *            the heightList to set
	 */
	public void setHeightList(List<SelectItem> heightList) {
		this.heightList = heightList;
	}

	/**
	 * @return the startList
	 */
	public List<SelectItem> getStarList() {
		return starList;
	}

	/**
	 * @param startList
	 *            the startList to set
	 */
	public void setStarList(List<SelectItem> starList) {
		this.starList = starList;
	}

	/**
	 * @return the rasiList
	 */
	public List<SelectItem> getRasiList() {
		return rasiList;
	}

	/**
	 * @param rasiList
	 *            the rasiList to set
	 */
	public void setRasiList(List<SelectItem> rasiList) {
		this.rasiList = rasiList;
	}

	/**
	 * @return the maritalstatusList
	 */
	public List<SelectItem> getMaritalstatusList() {
		return maritalstatusList;
	}

	/**
	 * @param maritalstatusList the maritalstatusList to set
	 */
	public void setMaritalstatusList(List<SelectItem> maritalstatusList) {
		this.maritalstatusList = maritalstatusList;
	}

	/**
	 * @return the educationLevelList
	 */
	public List<SelectItem> getEducationLevelList() {
		return educationLevelList;
	}

	/**
	 * @param educationLevelList the educationLevelList to set
	 */
	public void setEducationLevelList(List<SelectItem> educationLevelList) {
		this.educationLevelList = educationLevelList;
	}

	/**
	 * @return the searchController
	 */
	public SearchController getSearchController() {
		return searchController;
	}

	/**
	 * @param searchController the searchController to set
	 */
	public void setSearchController(SearchController searchController) {
		this.searchController = searchController;
	}

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * @return the list
	 */
	public List<ProfileDTO> getList() {
		return list;
	}

	/**
	 * @param list the list to set
	 */
	public void setList(List<ProfileDTO> list) {
		this.list = list;
	}

	/**
	 * @return the filteredProfiles
	 */
	public List<ProfileDTO> getFilteredProfiles() {
		return filteredProfiles;
	}

	/**
	 * @param filteredProfiles the filteredProfiles to set
	 */
	public void setFilteredProfiles(List<ProfileDTO> filteredProfiles) {
		this.filteredProfiles = filteredProfiles;
	}

	/**
	 * @return the selectedProfile
	 */
	public ProfileDTO getSelectedProfile() {
		return selectedProfile;
	}

	/**
	 * @param selectedProfile the selectedProfile to set
	 */
	public void setSelectedProfile(ProfileDTO selectedProfile) {
		this.selectedProfile = selectedProfile;
	}

}
